package com.ys.otms.controller;

import com.ys.otms.services.IDeductionsServices;
import com.ys.otms.services.IShipServices;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.UploadUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/ship")
public class ShipController {

    private static Logger logger = LoggerFactory.getLogger(ShipController.class);

    @Autowired
    private IShipServices shipServices;

    @Autowired
    private IDeductionsServices iDeductionsServices;

    @RequestMapping(value = "/queryShipByName")
    @ResponseBody
    public JSONObject queryShipByName(HttpServletRequest request) {
        logger.info("ShipController queryShipByLat start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = shipServices.queryShipByName(request);
        } catch (Exception e) {
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询数据失败！");
            logger.error("ShipController queryShipByName error", e);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryShipByLat")
    @ResponseBody
    public JSONObject queryShipByLat(HttpServletRequest request) {
        logger.info("ShipController queryShipByLat start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = shipServices.queryShipByLat(request);
        } catch (Exception e) {
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询数据失败！");
            logger.error("ShipController queryShipByLat error", e);
        }
        return retObj;
    }

    /**
     * 查询全部自由运力(船名,有监控，无监控，已认证，未认证)
     * @param request
     * @return
     */
    @RequestMapping("/selectAllShips")
    @ResponseBody
    public JSONObject selectAllShipsQuery(HttpServletRequest request) {
        logger.info("ShipController selectAllShipsQuery start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectAllShips(request);
        } catch (Exception e) {
            logger.error("ShipController selectAllShipsQuery error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","查询数据失败！");
        }
        return jsonShips;
    }

    /**
     * 查询过期自由运力
     * @param request
     * @return
     */
    @RequestMapping("/selectBeOverdueShipInfo")
    @ResponseBody
    public JSONObject selectBeOverdueShipInfo(HttpServletRequest request) {
        logger.info("ShipController selectBeOverdueShipInfo start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectBeOverdueShipInfo(request);
        } catch (Exception e) {
            logger.error("ShipController selectBeOverdueShipInfo error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","查询数据失败！");
        }
        return jsonShips;
    }

    /**
     * 查询全部自由运力(船名,有监控，无监控，已认证，未认证)
     * @param request
     * @return
     */
    @RequestMapping("/selectShipInfosByShipId")
    @ResponseBody
    public JSONObject selectShipInfosByShipId(HttpServletRequest request) {
        logger.info("ShipController selectShipInfosByShipId start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectShipInfosByShipId(request);
        } catch (Exception e) {
            logger.error("ShipController selectShipInfosByShipId error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","查询数据失败！");
        }
        return jsonShips;
    }


    /**
     * 根据条件查询运力（有监控，无监控，已认证，未认证）
     * @param request
     * @return
     */
//    @RequestMapping("/selectShipsByCondition")
//    @ResponseBody
//    public JSONObject selectShipsByCondition(HttpServletRequest request) {
//        logger.info("ShipController selectShipsByCondition start");
//        JSONObject jsonShips = new JSONObject();
//        try {
//            jsonShips = shipServices.selectShipsByCondition(request);
//        } catch (Exception e) {
//            logger.error("ShipController selectShipsByCondition error", e);
//        }
//        return jsonShips;
//    }


//    /**
//     * 查询全部运力
//     * @param request
//     * @return
//     */
//    @RequestMapping("/selectShipInfosByAll")
//    @ResponseBody
//    public JSONObject selectShipInfosByAllQuery(HttpServletRequest request) {
//        logger.info("ShipController selectShipInfosByAllQuery start");
//        JSONObject jsonShips = new JSONObject();
//        try {
//            jsonShips = shipServices.selectShipInfosByAllQuery(request);
//        } catch (Exception e) {
//            logger.error("ShipController selectShipInfosByAllQuery error", e);
//        }
//        return jsonShips;
//    }


    /**
     * 删除自由运力
     * @param request
     * @return
     */
    @RequestMapping("/deleteShipsByShipid")
    @ResponseBody
    public JSONObject deleteShipsByShipid(HttpServletRequest request) throws Exception {
        logger.info("ShipController deleteShipsByShipid start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.deleteShipsByShipid(request);
        } catch (Exception e) {
            logger.error("ShipController deleteShipsByShipid error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","自由运力删除失败！");
        }
        return jsonShips;
    }


    /**
     *查询航行日志
     * @param request
     * @return
     */
    @RequestMapping("/selectShipSailLog")
    @ResponseBody
    public JSONObject selectShipSailLog(HttpServletRequest request) throws Exception {
        logger.info("ShipController selectShipSailLog start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectShipSailLog(request);
        } catch (Exception e) {
            logger.error("ShipController selectShipSailLog error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","航行日志查询失败！");
        }
        return jsonShips;
    }


    /**
     *查询航行轨迹
     * @param request
     * @return
     */
    @RequestMapping("/selectShipTrack")
    @ResponseBody
    public JSONObject selectShipTrack(HttpServletRequest request) throws Exception {
        logger.info("ShipController selectShipTrack start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectShipTrack(request);
        } catch (Exception e) {
            logger.error("ShipController selectShipTrack error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","查询航行轨迹失败！");
        }
        return jsonShips;
    }

    /**
     * 将船舶添加到自有运力
     * @param request
     * @return
     * @throws Exception
     */

    @RequestMapping("/insertShipAndByShipId")
    @ResponseBody
    public JSONObject insertShipAndByShipId(HttpServletRequest request) throws Exception{
            JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ShipController insertShipAndByShipId start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = iDeductionsServices.deductionsMethod(request,obj);
        } catch (Exception e) {
            logger.error("ShipController insertShipAndByShipId Exception",e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","新增数据失败！");
        }
        return jsonShips;
    }


    /**
     * 船名找船
     * @param request
     * @return
     */
    @RequestMapping("/selectAllShipsByShipName")
    @ResponseBody
    public JSONObject selectAllShipsByShipName(HttpServletRequest request) {

        logger.info("ShipController selectAllShipsByShipName start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.selectAllShipsByShipName(request);
        } catch (Exception e) {
            logger.error("ShipController selectAllShipsByShipName error", e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","查询数据失败！");
        }
        return jsonShips;
    }


    /**
     * 立即认证
     * @param request
     * @return
     * @throws Exception
     */

    @RequestMapping("/insertShipInfo")
    @ResponseBody
    public JSONObject insertShipInfo(HttpServletRequest request) throws Exception{
        logger.info("ShipController insertShipInfo start");
        JSONObject jsonShips = new JSONObject();
        try {
            jsonShips = shipServices.insertShipInfo(request);
        } catch (Exception e) {
            logger.error("ShipController insertShipInfo Exception",e);
            jsonShips.put("SUCCESS",false);
            jsonShips.put("MSG","新增数据失败！");
        }
        return jsonShips;
    }

    @RequestMapping("/queryRegion")
    @ResponseBody
    public JSONObject queryRegion(HttpServletRequest request) {
        logger.info("ShipController queryRegion start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = shipServices.queryRegionShip(request);
        } catch (Exception e) {
            retObj.put("state",false);
            logger.error("ShipController queryRegion error", e);
        }
        return retObj;
    }

    /**
     *批量上传船舶信息
     * @param request
     * @return
     */
    @RequestMapping("/uploadUtil")
    @ResponseBody
    public JSONObject uploadUtil(HttpServletRequest request) throws Exception{
        logger.info("ShipController uploadUtil start");
        JSONObject jsonShips =new JSONObject();
        try {
            JSONObject obj = UploadUtil.getUpload(request);
            if ("SUCCESS".equals(JsonUtil.getJSONObjectString(obj,"message"))){
                jsonShips = shipServices.uploadShipInfo(request,obj);
                jsonShips.put("message","SUCCESS");
            }else{
                jsonShips =obj;
            }
        } catch (Exception e) {
            jsonShips.put("SUCCESS",false);
            logger.error("ShipController uploadUtil error", e);
        }
        return jsonShips;
    }



}
